package com.qf.web.admin.controller;

import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.blog.dto.ArticleDTO;
import com.qf.api.blog.service.ArticleService;
import com.qf.api.blog.vo.ArticlePageVO;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.api.sys.mybatis.page.PageResult;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/23 22:04
 * @Description:
 **/
@RestController
@RequestMapping("/admin/article")
@ApiOperationLogTitle("文章模块")
@Tag(name = "07.文章管理")
public class AdminArticleController extends BaseController {

    @Resource
    private ArticleService articleService;

    /**
     * tagName: 标签名
     * categoryName: 分类名
     * articleTitle: 文章标题
     * isTop: 是否置顶
     * status: 文章状态
     **/
    @GetMapping
    @Operation(summary = "获取文章列表")
    public ResponseResult<PageResult<ArticlePageVO>> getArticleList(@RequestParam(value = "tagId",required = false) Integer tagId,
                                                     @RequestParam(value = "categoryId",required = false) Integer categoryId,
                                                     @RequestParam(value = "articleTitle",required = false) String articleTitle,
                                                     @RequestParam(value = "isTop",required = false) Integer isTop,
                                                     @RequestParam(value = "status",required = false) Integer status){
        startPage();
        List<ArticlePageVO> articlePage = articleService.getArticlePage(tagId, categoryId, articleTitle, isTop, status);
        return ResponseResult.success(getData(articlePage));
    }

    @PostMapping
    @Operation(summary = "发布文章")
    @ApiOperationLog(desc = "发布文章",businessType = BusinessTypeEnum.ADD)
    public ResponseResult<Boolean> addArticle(@RequestBody ArticleDTO articleDTO){
        return isOk(articleService.publishArticle(articleDTO));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新文章")
    @ApiOperationLog(desc = "更新文章",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> updateArticle(@PathVariable Long id, @RequestBody ArticleDTO articleDTO){
        return isOk(articleService.updateArticle(id, articleDTO));
    }

    /**
     * TODO 后续实现 type: 1 假删   2 真删除
     **/
    @DeleteMapping
    @Operation(summary = "删除文章")
    @ApiOperationLog(desc = "删除文章",businessType = BusinessTypeEnum.DELETE)
    public ResponseResult<Boolean> deleteArticle(@RequestBody List<Long> ids){
        return isOk(articleService.removeBatchByIds(ids));
    }

    /**
     * id: 文章ID
     * type: 修改类型     0 -- 修改置顶  1 -- 修改状态
     * value: 状态值
     **/
    @PutMapping("/{id}/{type}/{value}")
    @Operation(summary = "更新文章 置顶/状态")
    @ApiOperationLog(desc = "更新文章 置顶/状态",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> updateArticleStatus(@PathVariable @NotNull Long id,
                                                       @PathVariable @NotNull Integer type,
                                                       @PathVariable @NotNull Integer value){
        return isOk(articleService.updateArticleStatus(id, type, value));
    }


    /**
     * 通过文章Id获取单个文章
     **/
    @GetMapping("/{id}")
    @Operation(summary = "获取单个文章")
    public ResponseResult<ArticleDTO> getArticleById(@PathVariable Long id){
        return ResponseResult.success(articleService.getArticleById(id));
    }

    /**
     * 通过title判断标题是否重复
     **/
    @GetMapping("/check")
    @Operation(summary = "判断文章标题是否重复")
    public ResponseResult<Boolean> checkArticleTitle(@RequestParam(value = "title") String title){
        return ResponseResult.success(articleService.checkArticleTitle(title));
    }
}
